public class Main {
    public static void main(String[] args) {

    }
}


class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}


class Solution {
    public int rob(TreeNode root) {
        return dfs(root)[0];
    }

    int[] dfs(TreeNode root) {
        if (root == null) {
            return new int[]{0, 0};
        }

        int[] dpl = dfs(root.left);
        int[] dpr = dfs(root.right);
        int[] dp = new int[2];
        dp[0] = Math.max(root.val + dpl[1] + dpr[1], dpl[0] + dpr[0]);
        dp[1] = dpl[0] + dpr[0];
        return dp;
    }
}